package com.juqimiao.raccoon.commodity.domain;

import com.juqimiao.linn.context.ApplicationContextWrapperI;
import com.juqimiao.linn.exception.BusinessException;
import com.juqimiao.raccoon.commodity.domain.category.CategoryGroup;
import com.juqimiao.raccoon.commodity.domain.support.repository.CategoryGroupEntityRepI;

import org.modelmapper.ModelMapper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;


/**
 * @author Collin Cai
 */
@Component
public class CategoryGroupFactory {
    @Autowired
    private ApplicationContextWrapperI wrapper;
    private ModelMapper modelMapper = new ModelMapper();

    @Autowired
    public CategoryGroupFactory() {
        modelMapper.getConfiguration().setSkipNullEnabled(true);
    }

    public CategoryGroup createNew(String name,
                                   String spell,
                                   String groupType,
                                   String scenario,
                                   String submitId,
                                   int sortOrder) {
        CategoryGroup categoryGroup = wrapper.getBean(CategoryGroup.class);
        categoryGroup.setName(name);
        categoryGroup.setSpell(spell);
        categoryGroup.setGroupType(groupType);
        categoryGroup.setScenario(scenario);
        categoryGroup.setPublish(false);
        categoryGroup.setAuthorized(false);
        categoryGroup.setSubmitId(submitId);
        categoryGroup.setSortOrder(sortOrder);
        categoryGroup.setStatus("normal");
        categoryGroup.getPolicy().setCategoryGroup(categoryGroup);
        return categoryGroup;
    }

    public CategoryGroup create(CategoryGroupEntityRepI repository, String categoryGroupId) {
        CategoryGroup categoryGroup = wrapper.getBean(CategoryGroup.class);
        CategoryGroup recovered = repository.getById(categoryGroupId);
        if(recovered != null) {

            modelMapper.map(recovered, categoryGroup);

            categoryGroup.getPolicy().setCategoryGroup(categoryGroup);
        } else {
            throw new BusinessException(String.format("查询不到，类目组(id=%s)，请尝试其他类目组项", categoryGroupId));
        }
        return categoryGroup;
    }

    public CategoryGroup create(CategoryGroup group) {
        CategoryGroup categoryGroup = wrapper.getBean(CategoryGroup.class);
        modelMapper.map(group, categoryGroup);

        categoryGroup.getPolicy().setCategoryGroup(categoryGroup);
        return categoryGroup;
    }

    public List<CategoryGroup> createMany(CategoryGroupEntityRepI repository, String[] ids){
        List<CategoryGroup> groups = repository.getMany(ids);
        List<CategoryGroup> resultList = new ArrayList<>();

        if(groups != null && !groups.isEmpty()) {
            for(CategoryGroup group : groups) {
                CategoryGroup categoryGroup = wrapper.getBean(CategoryGroup.class);
                modelMapper.map(group, categoryGroup);
                categoryGroup.getPolicy().setCategoryGroup(categoryGroup);
                resultList.add(categoryGroup);
            }
        }

        return resultList;
    }

    public CategoryGroup getInstance(String id,
                                     String name,
                                     String spell,
                                     String groupType,
                                     String scenario,
                                     boolean publish,
                                     boolean authorized,
                                     String submitId,
                                     int sortOrder,
                                     String status) {
        CategoryGroup categoryGroup = wrapper.getBean(CategoryGroup.class);
        categoryGroup.setId(id);
        categoryGroup.setName(name);
        categoryGroup.setSpell(spell);
        categoryGroup.setGroupType(groupType);
        categoryGroup.setScenario(scenario);
        categoryGroup.setPublish(publish);
        categoryGroup.setAuthorized(authorized);
        categoryGroup.setSubmitId(submitId);
        categoryGroup.setSortOrder(sortOrder);
        categoryGroup.setStatus(status);
        categoryGroup.getPolicy().setCategoryGroup(categoryGroup);
        return categoryGroup;
    }
}
